<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/3/16
 * Time: 15:28
 */

namespace app\index\controller;


use app\admin\model\Active;
use app\admin\model\ChoujiangTimes;
use app\admin\model\Reward as RewardModel;
use app\admin\model\RewardLog;
use Library\Tools;
use think\Config;
use think\Controller;
use org\wechat\jssdk; 

class Reward extends Controller
{
    public function chou(){
        $param = $this->request->param();

        if(empty($param['active_id'])){
            return view('public/error',['message'=>'活动id丢失']);
        }

        $reward_model = new RewardModel();
        $list = $reward_model->where('active_id',$param['active_id'])->where('reward_type',1)->where('status',1)->select();

        $res = \Library\Reward::StartChouJiang($list);
        if($res){
            //print_r($res);die;
            //$res['is_endless'] == 0 说明奖品不是无限的,需要减1
            if($res['is_endless'] == 0){
                $reward_model->where('id',$res['id'])->setDec('current_number');
                echo $reward_model->getLastSql();
            }
            //$res['is_log'] == 1 说明奖品需要记录下来
            if($res['is_log'] == 1){
                $reward_code = '虚拟奖品无兑换码';
                if($res['reward_cate'] == 1){
                    $reward_code = Tools::random_number(6).$param['active_id'].mt_rand(1000,9999);
                }
                RewardLog::create([
                    'active_id' =>  $param['active_id'],
                    'reward_id' =>  $res['id'],
                    'user_id'   =>  'test-eyuyao',
                    'nickname'  =>  '测试人员',
                    'reward_code'=> $reward_code
                ]);
            }
            //记录抽奖次数
            $times_model = new ChoujiangTimes();
            $times = $times_model->where('active_id',$param['active_id'])->where('user_id',$param['openid'])->find();
            if($times){
                $times->times++;
                $times->save();
            }else{
                $times_model->create(['active_id'=>$param['active_id'],'user_id'=>$param['openid'],'times'=>1]);
            }
            return view('public/reward',['message'=>$res['tip_text']]);
        }

        return view('public/error',['message'=>'奖池是空的哦']);
    }

    public function chou_ajax(){
        $param = $this->request->param();

        if(empty($param['active_id'])){
            return json(['code'=>999,'message'=>'活动id丢失']);
        }
        if(empty($param['openid'])){
            return json(['code'=>999,'message'=>'用户丢失']);
        }
        if(empty($param['nickname'])){
            return json(['code'=>999,'message'=>'用户昵称丢失']);
        }

        //记录抽奖次数
        $times_model = new ChoujiangTimes();
        $times = $times_model->where('active_id',$param['active_id'])->where('user_id',$param['openid'])->find();
        $active = Active::where('id',$param['active_id'])->where('status',1)->find();
        if($active){
            if(!$active->is_draw){
                return json(['code'=>999,'message'=>'活动无抽奖']);
            }
        }else{
            return json(['code'=>999,'message'=>'活动不存在']);
        }
        if($times){
            if($active->draw_times <= $times->times){
                return json(['code'=>999,'message'=>'抽奖次数过多已屏蔽']);
            }
        }

        $reward_model = new RewardModel();
        $list = $reward_model->where('active_id',$param['active_id'])->where('reward_type',1)->where('status',1)->select();

        $res = \Library\Reward::StartChouJiang($list);
        if($res){
            //print_r($res);die;
            //$res['is_endless'] == 0 说明奖品不是无限的,需要减1
            if($res['is_endless'] == 0){
                $reward_model->where('id',$res['id'])->setDec('current_number');
            }
            //$res['is_log'] == 1 说明奖品需要记录下来
            if($res['is_log'] == 1){
                $reward_code = '虚拟奖品无兑换码';
                if($res['reward_cate'] == 1){
                    $reward_code = Tools::random_number(6).$param['active_id'].mt_rand(1000,9999);
                }
                RewardLog::create([
                    'active_id' =>  $param['active_id'],
                    'reward_id' =>  $res['id'],
                    'user_id'   =>  $param['openid'],
                    'nickname'  =>  $param['nickname'],
                    'reward_code'=> $reward_code,
                    'mobile'    => $param['mobile'],
                    'create_time'=> time(),
                ]);
            }

            if($times){
                $times->times++;
                $times->save();
            }else{
                $times_model->create(['active_id'=>$param['active_id'],'user_id'=>$param['openid'],'times'=>1]);
            }
            return json(['code'=>0,'message'=>$res['tip_text']]);
        }

        return json(['code'=>999,'message'=>'奖池是空的哦']);
    }

    public function receive(){
        $this->assign('controller_prefix',Config::get('controller_prefix'));
        $code = $this->request->get('code');
        $record = null;
        if($code){
            $record = RewardLog::where('reward_code',$code)->find();
        }

        return view('',['code'=>$code,'record'=>$record]);
    }

    public function do_receive(){
        $id = $this->request->get('id');
        if($id){
            $record = RewardLog::where('id',$id)->find();
            if($record){
                $record->is_receive = 1;
                $record->save();
                return json(['code'=>0,'message'=>'领取成功']);
            }
            return json(['code'=>999,'message'=>'领取失败']);
        }
        return json(['code'=>999,'message'=>'id丢失']);
    }
}